package pers.kw.controller;

import lombok.RequiredArgsConstructor;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pers.kw.config.ConfirmConfig;
import pers.kw.config.FirstQueueConfig;

import java.nio.charset.StandardCharsets;

@Slf4j
@Value
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/confirm")
public class ConfirmController {

    RabbitTemplate rabbitTemplate;

    /**
     * 测试消息投递Exchange，确认回调机制
     *
     * @param msg
     * @return
     */
    @GetMapping(value = "/sendMsg/{msg}")
    public String sendMsg(@PathVariable String msg) {
        log.info("发送的消息是：{}", msg);
        String id = "1";
        CorrelationData correlationData = new CorrelationData(id);
        // 发送消息
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME, ConfirmConfig.CONFIRM_ROUTING_KEY,
                msg.getBytes(StandardCharsets.UTF_8), correlationData);

        // 注意：消息 2 发送的交换机不存在，可以观察回调是否监听到
        id = "2";
        correlationData = new CorrelationData(id);
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME + "2", ConfirmConfig.CONFIRM_ROUTING_KEY,
                msg.getBytes(StandardCharsets.UTF_8), correlationData);

        return "发送消息";
    }

    /**
     * 确认消息投递队列，确认回退机制
     *
     * @param msg
     * @return
     */
    @GetMapping(value = "/sendMsg2/{msg}")
    public String sendMsg2(@PathVariable String msg) {
        log.info("发送的消息是：{}", msg);
        String id = "1";
        CorrelationData correlationData = new CorrelationData(id);
        // 发送消息
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME, ConfirmConfig.CONFIRM_ROUTING_KEY,
                msg.getBytes(StandardCharsets.UTF_8), correlationData);

        // 注意：消息 2 发送的交换机不存在，可以观察回调是否监听到
        id = "2";
        correlationData = new CorrelationData(id);
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME, ConfirmConfig.CONFIRM_ROUTING_KEY + "2",
                msg.getBytes(StandardCharsets.UTF_8), correlationData);

        return "发送消息";
    }

    /**
     * 优先级队列
     *
     * @param msg
     * @return
     */
    @GetMapping(value = "/firstSendMsg/{msg}")
    public String firstSendMsg(@PathVariable String msg) {
        log.info("发送的消息是：{}", msg);
        for (int i = 0; i < 10; i++) {
            if (i == 5) {
                MessagePostProcessor messagePostProcessor = message -> {
                    MessageProperties messageProperties = message.getMessageProperties();
                    // 设置消息的优先级
                    messageProperties.setPriority(5);
                    return message;
                };
                // 发送消息
                rabbitTemplate.convertAndSend(FirstQueueConfig.EXCHANGE_NAME, FirstQueueConfig.ROUTING_KEY, String.valueOf(i).getBytes(StandardCharsets.UTF_8), messagePostProcessor);
            }
            // 发送消息
            rabbitTemplate.convertAndSend(FirstQueueConfig.EXCHANGE_NAME, FirstQueueConfig.ROUTING_KEY, String.valueOf(i).getBytes(StandardCharsets.UTF_8));
        }
        return "发送消息";
    }
}
